#include "BlockQueue.hpp"
#include "ThreadTemplate.hpp"
#include <unistd.h>
using namespace BlockQueueModule;

void Productor(BlockQueue<int>& bq){
    int data = 10;
    int cnt = 19;
    while(cnt--){
        cout << "["<< pthread_self()<< "]" <<  "生产者生产了一个数据: " << data << endl;
        bq.Enqueue(data);
        data++;
        sleep(1);
    }
    cout << "["<< pthread_self()<< "]" << "生产完毕,准备退出" << endl;
}

void Consumer(BlockQueue<int>& bq){
    sleep(2); //先让生产者生产数据
    int cnt = 10;
    while(cnt--){
        int data;
        bq.Pop(&data);
        cout << "["<< pthread_self()<< "]" << "消费者消费了一个数据: " << data << endl;
        sleep(1);
    }
    cout << "["<< pthread_self()<< "]" << "消费完毕,准备退出" << endl;
}

int main(){
    
    BlockQueue<int>* bq = new BlockQueue<int>();

    Thread<BlockQueue<int>> p1(Productor,*bq);
    Thread<BlockQueue<int>> p2(Productor,*bq);
    Thread<BlockQueue<int>> c1(Consumer,*bq);
    Thread<BlockQueue<int>> c2(Consumer,*bq);
    Thread<BlockQueue<int>> c3(Consumer,*bq);

    p1.Start();
    p2.Start();
    c1.Start();
    c2.Start();
    c3.Start();



    p1.Join();
    p2.Join();
    c1.Join();
    c2.Join();
    c3.Join();

    return 0;
}